<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>更新应用</title>
<script src="js/jquery-1.9.1.min.js"></script>

<script src="js/jquery.mCustomScrollbar.concat.min.js"></script>
<link href="css/jquery.mCustomScrollbar.css" rel="stylesheet" type="text/css">

<script src="js/ajaxfileupload.js"></script>

<script src="js/rb.js"></script>
<link href="css/rb.css" rel="stylesheet" type="text/css">

<script src="js/dump.js"></script>
<script>
var Json;
Json = {'respCode':'0','respMessage':'请求成功','respBody':{'apkInfo':{'downloadUrl':'1411633382332_xiaodi.apk\u0026\u003d37b833fbfe594b8f2160857bbf393967','applicationIcon':'/rest/common/getAttachment?filePath\u003d1411633382583.png','versionName':'1.0','sdkVersion':'8'},'applicationInfo':{'id':'com.liudi.test1','files':[{}],'fragments':[{'host':'helloworld','name':'com.liudi.test.LiudiFragmentTest','code':'com.liudi.test1'}],'version':'20140721.1'}},'serverDate':'2014-09-25 16:23'};

$(document).ready(function() {
	
	$("#wai").css("width",FRAMEWIDTH-20+"px");
	$(".rb_app_icon").css("height",$(".rb_app_icon").width());
	$(".rb_button").css("top",$(".rb_app_icon").offset().top+$(".rb_app_icon").height()-30+"px");
	$(".info").css("margin-left",$(".rb_app_icon").width());
	$(".info").css("width",FRAMEWIDTH-$(".rb_app_icon").width());
	//$(".fragments").css("height", $(".info").height()-$(".info > ul").height()-150+"px");
	
	
	//监听文件上传
	$("#appFile").bind("change").change(function(e) {
		if ($(this).val().length>0){
			uploadFile();
		}
    });
	//监听应用名称
	$("#appName").bind("change").change(function(e) {
		if ($(this).val().length>0){
			$("#applicationName").val($(this).val());
		}
    });
	
	$("#applicationName").val(RB.Sstrorage(false,'appName'));
	$(".info").find("li:nth-child(1) span:nth-child(2)").text(RB.Sstrorage(false,'appName'));
	
});

function uploadFile(){
	var sdkStr="";
	$.ajaxFileUpload({		
        url: SERVER+"common/uploadApk",
        secureuri: false, //是否需要安全协议，一般设置为false
        fileElementId: 'appFile', //文件上传域的ID
        dataType: 'json', 
        contentType:"text/html" ,  
       	success: function(json){
			Json = json;
			
			RB.Toast("上传APK<b>\"成功\"</b>！");			
			$(".info").find("li:nth-child(2) span:nth-child(2)").text(Json.respBody.apkInfo.versionName);
			switch (Json.respBody.apkInfo.sdkVersion){
				case "8":
					sdkStr = "Android 2.2版本及以上";
				break;
				case "9":
					sdkStr = "Android 2.3版本及以上";
				break;
				case "10":
					sdkStr = "Android 2.3.3版本及以上";
				break;
				case "11":
					sdkStr = "Android 3.0版本及以上";
				break;
				default:
					sdkStr = "未知Android版本";
			}	
			$(".rb_app_icon").find("img").attr("src",IMGURL+Json.respBody.apkInfo.applicationIcon); 
			$(".info").find("li:nth-child(3) span:nth-child(2)").text(sdkStr);
			for (var i in Json.respBody.applicationInfo.fragments){
				$(".fragments").append("<p>Host："+Json.respBody.applicationInfo.fragments[i].host+"</br>Name："+Json.respBody.applicationInfo.fragments[i].name+"</br>Code："+Json.respBody.applicationInfo.fragments[i].code+"</p>");
				
				$("#saveForm").append("<input type='text'  name='appFragments["+i+"].host' value='"+Json.respBody.applicationInfo.fragments[i].host+"'/>");
				$("#saveForm").append("<input type='text'  name='appFragments["+i+"].name' value='"+Json.respBody.applicationInfo.fragments[i].name+"'/>");
				$("#saveForm").append("<input type='text'  name='appFragments["+i+"].code' value='"+Json.respBody.applicationInfo.fragments[i].code+"'/>");
			}
			
			$("#versionName").val(Json.respBody.apkInfo.versionName);
			$("#iconUrl").val(Json.respBody.apkInfo.applicationIcon);
			$("#downloadUrl").val(Json.respBody.apkInfo.downloadUrl);
				
			$(".rb_app_icon").animate({left:30,top:30,width:150,height:150},500);
			$("#uploadBtn").fadeOut();
			$(".info").delay(500).fadeIn();
			$(".fragments").mCustomScrollbar({
				theme:"dark",
				advanced:{updateOnContentResize:true}
			});
        },
        error: function(data, status, e){
            alert(e);
            RB.Toast("上传APK<b>\"失败\"</b>，请重试！");	
    	}
	});	
}
function sumbitApp(){
	$.ajax({
		timeout:2000, 
		type: AJAXMETHOD,
		async: false,
		url: SERVER+"applicationmanager/"+RB.Sstrorage(false,'appId'),
		data: $('#saveForm').serialize(),
		dataType: AJAXJSON,
		jsonp: "callback",
		jsonpCallback:"callback",
		success: function(json){
			RB.AlertInfo("更新应用成功","点击确定返回已有应用列表","window.location.href='yyyy.html';");			
		},
		error: function(){
			RB.Toast("更新应用<b>\"失败\"</b>，请重试！");	
		}
	});
	//RB.AlertInfo("添加应用成功","点击确定返回已有应用列表","window.location.href='yyyy.html';");	
}
</script>
<style>
#wai{	
	position:absolute;
	background:url(assets/app_new_bg.gif);
}

.rb_mainframe_top_title_first{
	background-color:#f2638f;
	background-image:url(assets/top_bar_first_item_bg2.png);	
}

.rb_mainframe_middle_title{
	background-color:#6e8187;
}

.rb_app_icon{
	position:absolute;
	padding:4px 4px 4px 4px;
	border:#999 1px solid;
	border-radius:10px;
	background-color:#23cdb7;
	width:20%;
	top:20%;
	left:40%;
}

.rb_app_icon img{
	margin-left:auto;
	margin-right:10px;
	width:100%;
	height:100%;
	border-radius:10px;
}
#uploadBtn{
	position:absolute;
	width:20%;
	left:40%;
	padding:4px 4px 4px 4px;
}
.info{
	height:100%;
	width:100%;
	overflow:hidden;
	display:none;
}
.info > ul{
	margin-right:20px;
	padding:20px;
}
.info > ul > li{
	width:100%;
	list-style-type:none;	
	height:50px;
	line-height:50px;
	color:#000;
	vertical-align:middle;
}
.info > ul > li > span:first-child{
	font-size:large;
	color:#999;
}

.info > ul > li > span:nth-child(2){
	font-weight:bold;
	font-size:large;
	color:#333;
}

.fragments{
	width:90%;
	height:40%;
	padding-left:30px;
	color:#999;
	font-size:large;
}

.fragments  p{
	border:0px;
	font-size:medium;
	color:#333;
	border-bottom:#CCC 1px solid;
}
fragments p:last-child{
	border:0px;
	border-bottom:#CCC 0px solid;
}
#submit{
	width:20%;
	padding:4px 4px 4px 4px;
}

</style>
</head>

<body onselectstart="return false">
<!--topbar--> 
<div class="rb_mainframe_top">
	<div class="rb_mainframe_top_title_first">应用中心</div>
    <div class="rb_mainframe_top_title_second">更新应用</div>
    <div class="rb_mainframe_top_btn">帮助</div>
    <div class="rb_mainframe_top_btn" onClick="dump(Json)">查看</div>
</div>
<!--content--> 
<div class="rb_mainframe_content">
    <div class="rb_mainframe_middle" id="wai">
        <div class="rb_app_icon">
            <img src="assets/icon_android_big.png"/>  
        </div>
        <div class="rb_button" id="uploadBtn"><input type="button" value="请选择要更新应用的APK文件" onClick="appFile.click()" style="background-color:#F0F;"><form id="appForm" name="appForm" enctype="multipart/form-data" method="post" target="_blank"><input id="appFile" type="file" name="appFile" style="display:none;" accept=".apk"><input id="name" type="text" name="name" style="display:none;"></form>
        </div> 
        
        <div class="info">
        	<ul>
            	<li><span>应用名称：</span><span></span></li>
                <li><span>版本：</span><span></span></li>
                <li><span>设备版本要求：</span><span></span></li>
            </ul>
            <div class="fragments">
            	<div>Fragemts列表：</div>
            </div>
            <div class="rb_button" id="submit"><input type="button" value="确定更新" onClick="sumbitApp()"/></div>
        </div>  	
    </div>
</div>
<div style="display:none;">
	<form method="post" id="saveForm">
		<input type="text" id="versionName" name="versionName" value=""/>
        <input type="text" id="applicationName" name="applicationName" value=""/>
        <input type="text" id="downloadUrl" name="downloadUrl" value="" />
    	<input type="text" id="iconUrl" name="iconUrl" value=""/>
		<input type="text" id="platformType" name="platformType" value="android" />
    	<input type="text" id="isValid" name="isValid" value="1" />
    	<!--
    	<input type="text"  name="appFragments[0].host" value=""/>
		<input type="text"  name="appFragments[0].name" value=""/>
		<input type="text"  name="appFragments[0].code" value=""/>
        -->
	</form>
</div>     
</body>
</html>
